using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class NXHANGDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procNXHANG_add]";
        private string _strSPUpdateName = "dbo.[procNXHANG_update]";
        private string _strSPDeleteName = "dbo.[procNXHANG_delete]";
        private string _strSPGetName = "dbo.[procNXHANG_get]";
        private string _strSPGetAllName = "dbo.[procNXHANG_getall]";
        private string _strSPGetPages = "dbo.[procNXHANG_getpaged]";
        private string _strSPIsExist = "dbo.[procNXHANG_isexist]";
        private string _strTableName = "[NXHANG]";
        private string _strSPGetTransferOutName = "dbo.[procNXHANG_gettransferout]";
        string _strSPGetCountName = "procNXHANG_getcount";
        string _strSPGetByIndexName = "procNXHANG_getindex";
        #endregion Local Variable
        string _strSPGetByAllByCT = "procNXHANG_getAllByCT";
        #region Method
        public NXHANGInfo Get(
        String SOCT,
        Int32 LINE,
        String LOAICT,
        DateTime NGAYCT,
        ref string sErr)
        {
            NXHANGInfo objEntr = new NXHANGInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter(NXHANGInfo.Field.SOCT.ToString(), SOCT);
            AddParameter(NXHANGInfo.Field.LINE.ToString(), LINE);
            AddParameter(NXHANGInfo.Field.LOAICT.ToString(), LOAICT);
            AddParameter(NXHANGInfo.Field.NGAYCT.ToString(), NGAYCT);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            if (list.Rows.Count > 0)
                objEntr = new NXHANGInfo(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetByPos(
        int pos, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetByIndexName);
            AddParameter("INX", pos);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public int GetCountRecord(
        ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPGetCountName);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(NXHANGInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPInsertName);
            AddParameter(NXHANGInfo.Field.SOCT.ToString(), objEntr.SOCT);
            AddParameter(NXHANGInfo.Field.LINE.ToString(), objEntr.LINE);
            AddParameter(NXHANGInfo.Field.LOAICT.ToString(), objEntr.LOAICT);
            AddParameter(NXHANGInfo.Field.NGAYCT.ToString(), objEntr.NGAYCT);
            AddParameter(NXHANGInfo.Field.LOAIHANG.ToString(), objEntr.LOAIHANG);
            AddParameter(NXHANGInfo.Field.LOCATION.ToString(), objEntr.LOCATION);
            AddParameter(NXHANGInfo.Field.QUYCACH.ToString(), objEntr.QUYCACH);
            AddParameter(NXHANGInfo.Field.DVTINH.ToString(), objEntr.DVTINH);
            AddParameter(NXHANGInfo.Field.SOLUONG.ToString(), objEntr.SOLUONG);
            AddParameter(NXHANGInfo.Field.GIAVON.ToString(), objEntr.GIAVON);
            AddParameter(NXHANGInfo.Field.GIABAN.ToString(), objEntr.GIABAN);
            AddParameter(NXHANGInfo.Field.SOTIEN.ToString(), objEntr.SOTIEN);
            AddParameter(NXHANGInfo.Field.TINHTRANG.ToString(), objEntr.TINHTRANG);
            AddParameter(NXHANGInfo.Field.XUATKHO.ToString(), objEntr.XUATKHO);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(NXHANGInfo objEntr)
        {
            connect();
            InitSPCommand(_strSPUpdateName);
            AddParameter(NXHANGInfo.Field.SOCT.ToString(), objEntr.SOCT);
            AddParameter(NXHANGInfo.Field.LINE.ToString(), objEntr.LINE);
            AddParameter(NXHANGInfo.Field.LOAICT.ToString(), objEntr.LOAICT);
            AddParameter(NXHANGInfo.Field.NGAYCT.ToString(), objEntr.NGAYCT);
            AddParameter(NXHANGInfo.Field.LOAIHANG.ToString(), objEntr.LOAIHANG);
            AddParameter(NXHANGInfo.Field.LOCATION.ToString(), objEntr.LOCATION);
            AddParameter(NXHANGInfo.Field.QUYCACH.ToString(), objEntr.QUYCACH);
            AddParameter(NXHANGInfo.Field.DVTINH.ToString(), objEntr.DVTINH);
            AddParameter(NXHANGInfo.Field.SOLUONG.ToString(), objEntr.SOLUONG);
            AddParameter(NXHANGInfo.Field.GIAVON.ToString(), objEntr.GIAVON);
            AddParameter(NXHANGInfo.Field.GIABAN.ToString(), objEntr.GIABAN);
            AddParameter(NXHANGInfo.Field.SOTIEN.ToString(), objEntr.SOTIEN);
            AddParameter(NXHANGInfo.Field.TINHTRANG.ToString(), objEntr.TINHTRANG);
            AddParameter(NXHANGInfo.Field.XUATKHO.ToString(), objEntr.XUATKHO);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String SOCT,
        Int32 LINE,
        String LOAICT,
        DateTime NGAYCT
        )
        {
            connect();
            InitSPCommand(_strSPDeleteName);
            AddParameter(NXHANGInfo.Field.SOCT.ToString(), SOCT);
            AddParameter(NXHANGInfo.Field.LINE.ToString(), LINE);
            AddParameter(NXHANGInfo.Field.LOAICT.ToString(), LOAICT);
            AddParameter(NXHANGInfo.Field.NGAYCT.ToString(), NGAYCT);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTableCollection Get_Page(NXHANGInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            string whereClause = CreateWhereClause(obj);
            DataTableCollection dtList = null;
            InitConnect();
            InitSPCommand(_strSPGetPages);

            AddParameter("WhereClause", whereClause);
            AddParameter("OrderBy", orderBy);
            AddParameter("PageIndex", pageIndex);
            AddParameter("PageSize", pageSize);

            try
            {
                dtList = executeCollectSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }

        public Boolean IsExist(
        String SOCT,
        Int32 LINE,
        String LOAICT,
        DateTime NGAYCT
        )
        {
            connect();
            InitSPCommand(_strSPIsExist);
            AddParameter(NXHANGInfo.Field.SOCT.ToString(), SOCT);
            AddParameter(NXHANGInfo.Field.LINE.ToString(), LINE);
            AddParameter(NXHANGInfo.Field.LOAICT.ToString(), LOAICT);
            AddParameter(NXHANGInfo.Field.NGAYCT.ToString(), NGAYCT);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(NXHANGInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            InitConnect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method
        public DataTable GetByAllByCT(
           String SOCT,String LOAICT,
           DateTime NGAYCT,
           ref string sErr)
        {
            NXHANGInfo objEntr = new NXHANGInfo();
            InitConnect();
            InitSPCommand(_strSPGetByAllByCT);
            AddParameter(NXHANGInfo.Field.SOCT.ToString(), SOCT);
            AddParameter(NXHANGInfo.Field.LOAICT.ToString(), LOAICT);
            AddParameter(NXHANGInfo.Field.NGAYCT.ToString(), NGAYCT);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            //if (list.Rows.Count > 0)
            //    objEntr = new NXHANGInfo(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
